/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package canteen.sys.view;

import canteen.sys.model.entity.Item;
import canteen.sys.model.generic.HibernateDAOFactory;
import java.util.List;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author tntwa_000
 */
public class BookReport extends javax.swing.JInternalFrame {

    HibernateDAOFactory hibernateDAOFactory = HibernateDAOFactory.getInstance();
    DefaultTableModel modelTableItem;

    /**
     * Creates new form BookReport
     */
    public BookReport() {
        initComponents();
        modelTableItem = (DefaultTableModel) tableBook.getModel();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        txtname = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tableBook = new javax.swing.JTable();
        cb = new javax.swing.JComboBox();

        setClosable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("Thống kê sách nước ngoài theo nơi lưu trữ");
        setToolTipText("");

        jButton1.setText("Thống kê");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        tableBook.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null}
            },
            new String [] {
                "Số ký hiệu", "Tên tác giả", "Tên tác phẩm", "Lần sản xuất", "Nhà sản xuất", "Ngôn ngữ", "Chủ sở hữu", "Thỏa thuận"
            }
        ));
        jScrollPane1.setViewportView(tableBook);

        cb.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Sách", "Tạp Chí" }));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(txtname, javax.swing.GroupLayout.PREFERRED_SIZE, 242, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cb, javax.swing.GroupLayout.PREFERRED_SIZE, 183, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 164, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 293, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1)
                    .addComponent(cb, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        List<Item> listItem = null;
        if (cb.getSelectedIndex() == 0) {
            listItem = hibernateDAOFactory.getItemDAO().findAllItemByStore(txtname.getText(), true, true);

        } else {
            listItem = hibernateDAOFactory.getItemDAO().findAllItemByStore(txtname.getText(), false, true);
        }
        showList(listItem);

    }//GEN-LAST:event_jButton1ActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cb;
    private javax.swing.JButton jButton1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tableBook;
    private javax.swing.JTextField txtname;
    // End of variables declaration//GEN-END:variables

    public final void showList(List<Item> list) {
        try {
            int totalrow = tableBook.getRowCount();
            if (totalrow > 0) {
                for (int i = 0; i < totalrow; i++) {
                    modelTableItem.removeRow(0);
                }
            }
            if (list != null && list.size() > 0) {
                for (Item item : list) {
                    modelTableItem.insertRow(0, new Object[]{item.getOtherCode(),
                                item.getAuthor().getName(), item.getName(), item.getVersion(),
                                item.getOwnerItem().getNameOfOwner(), item.getLanguage().getName(),
                                item.getTypeOfUse().getName(), item.getTypeOfRegulation().getName()});
                }
            }
        } catch (Exception ex) {
        }


//    private void reportBook() {
//        try {
//
//            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//            Connection conn = DriverManager
//                    .getConnection("jdbc:mysql://localhost:3307/dbtailieu?zeroDateTimeBehavior=convertToNull",
//                    "root", "cuocsongvuive");
//
//            // TODO add your handling code here:
//
//            String reportName = "BookReport.jasper";
//            HashMap map = new HashMap();
//            //map.put("name", jTextField1.getText());
//            //map.put("date","10/01/2012");
//            InputStream is = this.getClass().getClassLoader().getResourceAsStream("canteen/sys/view/resource/" + reportName);
//            JasperPrint jasperPrint = JasperFillManager.fillReport(is, map, conn);
//            JasperViewer jv = new JasperViewer(jasperPrint, false);
//            jv.setVisible(true);
//        } catch (JRException ex) {
//            Logger.getLogger(BookReport.class.getName()).log(Level.SEVERE, null, ex);
////            JOptionPane.showMessageDialog(null,"jr"+ex.getMessage()); 
//        } catch (Exception ex) {
//            System.out.println(ex.getMessage());
//        }
//    }
    }
}
